package com.zp.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.system.entity.bo.SysChooseRoleGroupBo;
import com.zp.system.entity.vo.SysChooseRoleGroupVo;
import com.zp.system.service.ISysChooseRoleGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 角色与已选择自定义服务对照控制器
 * 前端访问路由地址为:/system/chooseRoleGroup
 *
 * @author zhang peng
 * @date 2022-12-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/chooseRoleGroup")
public class SysChooseRoleGroupController extends BaseController {

    private final ISysChooseRoleGroupService iSysChooseRoleGroupService;

    /**
     * 查询角色与已选择自定义服务对照列表（不分页）
     */
    @SaCheckPermission("system:chooseRoleGroup:list")
    @GetMapping("/list")
    public R<List<SysChooseRoleGroupVo>> list (SysChooseRoleGroupBo bo) {
        List<SysChooseRoleGroupVo> list = iSysChooseRoleGroupService.queryList(bo);
        return R.ok(list);
    }


    /**
     * 获取角色与已选择自定义服务对照详细信息
     *
     * @param roleId 主键
     */
    @SaCheckPermission("system:chooseRoleGroup:query")
    @GetMapping("/{roleId}")
    public R<SysChooseRoleGroupVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String roleId) {
        return R.ok(iSysChooseRoleGroupService.queryById(roleId));
    }

    /**
     * 新增角色与已选择自定义服务对照
     */
    @SaCheckPermission("system:chooseRoleGroup:add")
    @Log(title = "已选择服务对照", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody SysChooseRoleGroupBo bo) {
        return toAjax(iSysChooseRoleGroupService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改角色与已选择自定义服务对照
     */
    @SaCheckPermission("system:chooseRoleGroup:edit")
    @Log(title = "已选择服务对照", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody SysChooseRoleGroupBo bo) {
        return toAjax(iSysChooseRoleGroupService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除角色与已选择自定义服务对照
     *
     * @param roleIds 主键串
     */
    @SaCheckPermission("system:chooseRoleGroup:remove")
    @Log(title = "已选择服务对照", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] roleIds) {
        return toAjax(iSysChooseRoleGroupService.deleteWithValidByIds(Arrays.asList(roleIds), true) ? 1 : 0);
    }

    @PostMapping("saveBatch")
    @Log(title = "已选择服务对照", businessType = BusinessType.INSERT)
    public R<Void> saveBatch (@RequestBody SysChooseRoleGroupBo bo) {
        return toAjax(iSysChooseRoleGroupService.saveBatch(bo));
    }

    @PostMapping("delChooseRoleGroup")
    @Log(title = "已选择服务对照", businessType = BusinessType.DELETE)
    public R<Void> delChooseRoleGroup (@RequestBody SysChooseRoleGroupBo bo) {
        return toAjax(iSysChooseRoleGroupService.delChooseRoleGroup(bo));
    }
}
